package logic

import (
	"context"
	"errors"
	"video-joy/models/mysql"
	"video-joy/userrpc/internal/svc"
	"video-joy/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserNameLogic {
	return &UpdateUserNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 昵称修改
func (l *UpdateUserNameLogic) UpdateUserName(in *userrpc.UpdateUserNameRequest) (*userrpc.UpdateUserNameResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo

	if err := user.UserFindByPhone(mysql.DB, in.PhoneNum); err != nil {
		return nil, errors.New("该用户不存在")
	}

	if err := user.UpdateUserName(mysql.DB); err != nil {
		return nil, errors.New("修改昵称失败")
	}

	return &userrpc.UpdateUserNameResponse{
		UserId: int64(user.ID),
	}, nil
}
